package com.thread.test04;

import java.io.*;

/**
 * @author bin
 * @date 2025 - 03 -07
 **/
public class Guard extends Thread{
    private double pace; // 进度
    public Guard(){

    }
    public Guard(double pace){
        this.pace = pace;
    }
    public double getPace() {
        return pace;
    }

    public void setPace(double pace) {
        this.pace = pace;
    }

    @Override
    public void run() {
        String source = "D:\\workpare\\classEx3\\src\\com\\thread\\test04\\source.txt";
        String data = "D:\\workpare\\classEx3\\src\\com\\thread\\test04\\data.txt";
        try {
            FileInputStream fis = new FileInputStream(source);
            FileOutputStream fos = new FileOutputStream(data);
            byte[] b = new byte[100];
            int len;
            int total = 0;
            long fileSize = new File(source).length();
            while((len = fis.read(b))!=-1){
                fos.write(b , 0 , len);
                total += len; 
                this.pace = (double) total / fileSize * 100; 
                System.out.printf("拷贝中... 进度: %.2f%%\r", pace); 
                if (pace >= 100) {
                    break;
                }
            }
            fos.flush(); // 清空缓冲区
            fos.close();
            fis.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
